import 'package:flutter/material.dart';
import 'package:basic_library/basic_library.dart';

///
/// 按钮集合
///
class QArrayPanelButton extends StatelessWidget {
  final List titles;
  final Widget? leftWidget;
  final double horizontalWidth; // 横向距离
  final Function(String name)? onTap;

  const QArrayPanelButton({
    Key? key,
    required this.horizontalWidth,
    required this.titles,
    this.leftWidget,
    this.onTap,
  }) : super(key: key);

  _buttonWidget() {
    Widget? temp;
    if (titles.length <= 2) {
      temp = Row(
        children: titles.map((e) {
          return Container(
            padding: const EdgeInsets.only(left: 16),
            child: QLineBorderBtn(
              title: e,
              onTap: () {
                if (onTap != null) {
                  onTap!.call(e);
                }
              },
            ),
          );
        }).toList(),
      );
      return temp;
    }
    List endButtons = [];
    List moreButtons = [];
    endButtons.add(titles[0]);
    endButtons.add(titles[1]);
    for (var element in titles) {
      if (titles.indexOf(element) != 0 && titles.indexOf(element) != 1) {
        moreButtons.add(element);
      }
    }
    temp = Row(
      children: [
        QMenuAlert(
          child: const Text(
            "更多",
            style: TextStyle(fontSize: 16, color: Color(0xFF177FF3)),
          ),
          titles: moreButtons,
          containerWidth: 110,
          horizontalWidth: horizontalWidth,
          tapCallback: (idx) {
            debugPrint("选择的按钮 = $idx");
            if (onTap != null) {
              onTap!.call(moreButtons[idx]);
            }
          },
        ),
        const SizedBox(width: 12),
        Row(
          children: endButtons.map((e) {
            return Container(
              padding: const EdgeInsets.only(left: 16),
              child: QLineBorderBtn(
                title: e,
                onTap: () {
                  if (onTap != null) {
                    onTap!.call(e);
                  }
                },
              ),
            );
          }).toList(),
        )
      ],
    );
    return temp;
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [leftWidget ?? Container(), _buttonWidget()],
    );
  }
}
